package com.ctrl.riveradministrator.mvp.view.activity.work_dynamic;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseActivity;
import com.ctrl.riveradministrator.base.BaseListAdapter;
import com.ctrl.riveradministrator.mvp.model.WorkDynamic;
import com.ctrl.riveradministrator.mvp.presenter.WorkDynPresenter;

import java.util.ArrayList;
import java.util.List;

public class WorkDynActivity extends BaseActivity<WorkDynPresenter> implements WorkDynView,
        BaseListAdapter.OnItemClickListener<WorkDynamic> {

    private Toolbar toolbar;
    private TextView titleTv;
    private ImageView leftMenu;

    private WorkDynAdapter workDynAdapter;
    private RecyclerView workDynRl;
    private SwipeRefreshLayout workDynSfl;
    private LinearLayoutManager layoutManager;

    private List<WorkDynamic> workDynamics = new ArrayList<>();
    private static int currentPage;

    @Override
    protected void initParams(Bundle parms) {

    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_work_dyn;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView(View view) {
        toolbar = $(R.id.toolbar);
        titleTv = $(R.id.title_tv);
        leftMenu = $(R.id.left_iv);
        workDynRl = $(R.id.policy_list);
        workDynSfl = $(R.id.policy_srl);

        layoutManager = new LinearLayoutManager(this);
        workDynRl.setLayoutManager(layoutManager);
        workDynSfl.setColorSchemeColors(Color.RED, Color.BLUE);
        workDynSfl.setEnabled(false);
        workDynAdapter = new WorkDynAdapter(this);
        workDynRl.setAdapter(workDynAdapter);

        pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
        pDialog.setTitleText("加载中...");
        pDialog.setCancelable(false);

        /*
         * 设置toolbar
         * */
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("");
        titleTv.setText(getString(R.string.work_dyn));
        leftMenu.setBackground(getDrawable(R.drawable.ic_chevron_left_24dp));
    }

    @Override
    protected void doBusiness(Context mContext) {
        currentPage = 0;
        workDynRl.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (workDynRl.computeVerticalScrollOffset() > 0&&workDynRl.computeVerticalScrollExtent() + workDynRl.computeVerticalScrollOffset()
                            >= workDynRl.computeVerticalScrollRange()) {
                        currentPage++;
                        mPresenter.getDynInfo(currentPage+"", "20");
                    }
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        mPresenter.getDynInfo(currentPage+"", "20");
    }

    @Override
    protected void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.left_iv:
                finish();
                break;
        }
    }

    @Override
    protected void setListener() {
        leftMenu.setOnClickListener(this);
        workDynAdapter.setOnItemClickListener(this);
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    protected WorkDynPresenter createPresenter() {
        return new WorkDynPresenter(this);
    }

    @Override
    public void onItemClick(WorkDynamic workDynamic, int position) {
        startActivity(new Intent(this, WebActivity.class).putExtra("workdyn", workDynamic));
    }

    @Override
    public void showMessage(List<WorkDynamic> workDynamics) {
        this.workDynamics.addAll(workDynamics);
        workDynAdapter.updateListView(this.workDynamics);
    }

    @Override
    public void onFailed(String code, String msg) {

    }

    @Override
    public void showLoading() {
        super.showLoading();
        //pDialog.show();
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        //pDialog.cancel();
    }
}
